FIND_PROGRAM(TAR_EXECUTABLE "tar")
IF(NOT TAR_EXECUTABLE)
    MESSAGE(FATAL_ERROR "tar: command not found.")
ENDIF()

FUNCTION(CE_DOWNLOAD_PACKAGE PACKAGE_ROOT PACKAGE_NAME PACKAGE_URL PACKAGE_FILE)
	MESSAGE("\n**************************************************************\n"
			"Downloading ${PACKAGE_NAME}\n"
			"**************************************************************\n\n")
    FILE(DOWNLOAD ${PACKAGE_URL}
	 ${PACKAGE_FILE} STATUS PACKAGE_STATUS SHOW_PROGRESS)
    LIST(GET PACKAGE_STATUS 0 PACKAGE_STATUS_CODE)
    IF(NOT ${PACKAGE_STATUS_CODE} EQUAL 0)
	LIST(GET PACKAGE_STATUS 1 PACKAGE_ERROR_MESSAGE)
	MESSAGE(FATAL_ERROR ${PACKAGE_ERROR_MESSAGE})
    ENDIF()
ENDFUNCTION(CE_DOWNLOAD_PACKAGE)

FUNCTION(CE_UNPACK_PACKAGE PACKAGE_ROOT PACKAGE_NAME PACKAGE_URL PACKAGE_DST PACKAGE_RESULT)
	SET(_PACKAGE_RESULT ${PACKAGE_DST}/${PACKAGE_NAME})
	IF(NOT EXISTS ${_PACKAGE_RESULT})
		GET_FILENAME_COMPONENT(_PACKAGE_FILENAME ${PACKAGE_URL} NAME)
		SET(_PACKAGE_FILE ${PACKAGE_ROOT}/tmp/${_PACKAGE_FILENAME})
		IF(NOT EXISTS ${_PACKAGE_FILE})
			CE_DOWNLOAD_PACKAGE(${PACKAGE_ROOT} ${PACKAGE_NAME} ${PACKAGE_URL} ${_PACKAGE_FILE})
		ENDIF()
		STRING(REGEX REPLACE "([A-Za-z]):(.*)" "/\\1\\2" PACKAGE_FILE_UNIX_FORMAT ${_PACKAGE_FILE})
		EXECUTE_PROCESS(COMMAND ${TAR_EXECUTABLE} "-xvf" "${PACKAGE_FILE_UNIX_FORMAT}"
						WORKING_DIRECTORY ${PACKAGE_DST}
						RESULT_VARIABLE PACKAGE_UNPACK_RESULT
						OUTPUT_VARIABLE PACKAGE_UNPACK_OUTPUT)
		MESSAGE("\n**************************************************************\n"
				"Unpacking ${PACKAGE_NAME}\n"
				"**************************************************************\n\n")
		IF(PACKAGE_UNPACK_RESULT)
			MESSAGE(FATAL_ERROR "an error has occurred while trying to decompress the following archive: ${_PACKAGE_FILE}.\n")
		ENDIF()
		STRING(REGEX MATCH "[^/]*/" _PACKAGE_DIR ${PACKAGE_UNPACK_OUTPUT})
		SET(_OLD_PACKAGE_RESULT ${PACKAGE_DST}/${_PACKAGE_DIR})
		IF(NOT EXISTS ${_OLD_PACKAGE_RESULT})
			MESSAGE(FATAL_ERROR "unexpected behaviour")
		ENDIF()
		FILE(RENAME ${_OLD_PACKAGE_RESULT} ${_PACKAGE_RESULT})
	ENDIF()
	SET(${PACKAGE_RESULT} ${_PACKAGE_RESULT} PARENT_SCOPE)
ENDFUNCTION(CE_UNPACK_PACKAGE)

FUNCTION(CE_INSTALL_PACKAGE)
	SET(ONE_VALUE_ARGS PACKAGE_ROOT PACKAGE_NAME PACKAGE_URL)
	SET(MULTI_VALUE_ARGS CONF_ARGS)
    CMAKE_PARSE_ARGUMENTS(CE_INSTALL_PACKAGE "${OPTIONS}" "${ONE_VALUE_ARGS}" "${MULTI_VALUE_ARGS}" ${ARGN})
	IF(NOT (CE_INSTALL_PACKAGE_PACKAGE_ROOT AND CE_INSTALL_PACKAGE_PACKAGE_NAME AND CE_INSTALL_PACKAGE_PACKAGE_URL))
		MESSAGE(FATAL_ERROR "incomplete arguments")
	ENDIF()
	SET(_PACKAGE_DIR "${CE_INSTALL_PACKAGE_PACKAGE_ROOT}/pkg/${CE_INSTALL_PACKAGE_PACKAGE_NAME}")
	IF(NOT EXISTS ${_PACKAGE_DIR})
		CE_UNPACK_PACKAGE(${CE_INSTALL_PACKAGE_PACKAGE_ROOT}
						  ${CE_INSTALL_PACKAGE_PACKAGE_NAME}
						  ${CE_INSTALL_PACKAGE_PACKAGE_URL} 
						  "${CE_INSTALL_PACKAGE_PACKAGE_ROOT}/src"
						  _PACKAGE_RESULT)
		SET(CONFIGURE_ARGS "CFLAGS=${CMAKE_C_FLAGS} -std=gnu99"
						   "CXX=${CMAKE_CXX_COMPILER}"
						   "CXXFLAGS=${CMAKE_CXX_FLAGS}"
						   "CC=${CMAKE_C_COMPILER}"
						   "LDFLAGS=-L${CMAKE_FIND_ROOT_PATH}/usr/lib"
						   "${CE_INSTALL_PACKAGE_CONF_ARGS}"
						   "--prefix=${_PACKAGE_DIR}"
						   "--with-sysroot=${CMAKE_FIND_ROOT_PATH}"
		)

		MESSAGE("\n**************************************************************\n"
				"Configuring ${CE_INSTALL_PACKAGE_PACKAGE_NAME}\n"
				"**************************************************************\n\n")
		EXECUTE_PROCESS(COMMAND "${_PACKAGE_RESULT}/configure" ${CONFIGURE_ARGS}
						WORKING_DIRECTORY ${_PACKAGE_RESULT}
						RESULT_VARIABLE CONF_RESULT)
		IF(CONF_RESULT)
			MESSAGE(FATAL_ERROR "an error has occurred while trying to execute the configuration script in the following package: ${CE_INSTALL_PACKAGE_PACKAGE_NAME}")
		ENDIF()
		MESSAGE("\n**************************************************************\n"
				"Building ${CE_INSTALL_PACKAGE_PACKAGE_NAME}\n"
				"**************************************************************\n\n")
		EXECUTE_PROCESS(COMMAND "make"
						WORKING_DIRECTORY ${_PACKAGE_RESULT}
						RESULT_VARIABLE BUILD_RESULT)
		IF(BUILD_RESULT)
			MESSAGE(FATAL_ERROR "an error has occurred while building the following package: ${CE_INSTALL_PACKAGE_PACKAGE_NAME}")
		ENDIF()
		MESSAGE("\n**************************************************************\n"
				"Installing ${CE_INSTALL_PACKAGE_PACKAGE_NAME}\n"
				"**************************************************************\n\n")
		EXECUTE_PROCESS(COMMAND "make" "install"
						WORKING_DIRECTORY ${_PACKAGE_RESULT}
						RESULT_VARIABLE INSTALL_RESULT)
		IF(INSTALL_RESULT)
			MESSAGE(FATAL_ERROR "an error has occurred while installing the following package: ${CE_INSTALL_PACKAGE_PACKAGE_NAME}")
		ENDIF()
    ENDIF()
ENDFUNCTION(CE_INSTALL_PACKAGE)